package com.cloud.core.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cloud.core.R;


/**
 * Created by Tanghaibo
 * 2018/4/9
 * description:
 */

public abstract class CommonDialog extends Dialog implements DialogInterface.OnDismissListener {
    private Context mContext;

    protected View mView;

    protected OnClickListener clickListener;


    public CommonDialog(@NonNull Context context) {
        this(context, R.style.DialogStyle);
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
    }

    protected void initView() {
        this.setOnDismissListener(this);
        mView = LayoutInflater.from(mContext).inflate(resId(), null);
        setContentView(mView);
        setCancelable(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    protected abstract int resId();

    protected abstract void initEvent();

    @Override
    public void onDismiss(DialogInterface dialog) {
        mContext = null;
    }

    public interface OnClickListener {
        default void onLeftClick() {

        }

        void onRightClick();
    }
}
